<?php
/**
 *  后台管理系统-核心功能
 */
namespace app\bews\controller;
use app\bews\controller\Base;
use think\facade\Db;
use think\facade\View;

class Oyk extends Base{
	public function user_list(){
		$lists = Db::table('oyk_user')->order('add_time DESC,uid DESC')->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function user_add(){
		$req = request();
		if($req->isPost()){
			$data['phone'] = trim(input('post.phone'));
			if(empty($data['phone'])){
				$this->returnCode(1,'手机号不能为空');
			}
			$pattern = "/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/";
			if(!preg_match($pattern,$data['phone'])){
				$this->returnCode(1,'请输入正确的手机号');
			}
			$pwd = trim(input('post.pwd'));
			if(empty($pwd)){
				$this->returnCode(1,'密码不能为空');
			}else{
				$data['pwd'] = md5($pwd);
			}
			$data['nickname'] = trim(input('post.nickname'));
			$data['sex'] = (int)trim(input('post.sex'));
			$data['status'] = (int)trim(input('post.status'));
			$data['add_time'] = time();
			$data['last_time'] = time();
			$res = Db::table('oyk_user')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	public function user_edit(){
		$req = request();
		if($req->isPost()){
			$uid = (int)input('post.uid');
			$data['phone'] = trim(input('post.phone'));
			if(empty($data['phone'])){
				$this->returnCode(1,'手机号不能为空');
			}
			$pattern = "/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/";
			if(!preg_match($pattern,$data['phone'])){
				$this->returnCode(1,'请输入正确的手机号');
			}
			$pwd = trim(input('post.pwd'));
			if(!empty($pwd)){
				$data['pwd'] = md5($pwd);
			}
			$data['nickname'] = trim(input('post.nickname'));
			$data['sex'] = (int)trim(input('post.sex'));
			$data['status'] = (int)trim(input('post.status'));
			$res = Db::table('oyk_user')->where('uid',$uid)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$uid = (int)input('get.uid');
			$find = Db::table('oyk_user')->where('uid',$uid)->find();
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	public function class_list(){
		$lists = Db::table('oyk_school_class')->order('add_time DESC,id DESC')->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function class_add(){
		$req = request();
		if($req->isPost()){
			$data['title'] = trim(input('post.title'));
			if(empty($data['title'])){
				$this->returnCode(1,'标题不能为空');
			}
			$data['school_id'] = (int)trim(input('post.school_id',0));
			if(empty($data['school_id'])){
				$this->returnCode(1,'请选择学校');
			}
			$data['address'] = trim(input('post.address'));
			if(empty($data['address'])){
				$this->returnCode(1,'请输入上课地址');
			}
			$data['tuition'] = (int)trim(input('post.tuition',0));
			$data['people_num'] = (int)trim(input('post.people_num',0));
			$data['teacher_name'] = trim(input('post.teacher_name'));
			if(empty($data['teacher_name'])){
				$this->returnCode(1,'老师姓名不能为空');
			}
			$data['teacher_phone'] = trim(input('post.teacher_phone'));
			if(empty($data['teacher_phone'])){
				$this->returnCode(1,'老师电话不能为空');
			}
			$data['class_hour'] = (int)trim(input('post.class_hour',0));
			$data['class_start_date'] = trim(input('post.class_start_date'));
			if(empty($data['class_start_date'])){
				$this->returnCode(1,'请选择开始上课日期');
			}
			$data['class_time'] = trim(input('post.class_time'));
			if(empty($data['class_time'])){
				$this->returnCode(1,'请输入上课周期');
			}
			$data['class_end_date'] = trim(input('post.class_end_date'));
			if(empty($data['class_end_date'])){
				$this->returnCode(1,'请选择报名截止日期');
			}
			$pic = input('post.pic');
			if(!empty($pic)){
				$data['intr'] = implode(';',$pic);
			}
			$data['status'] = (int)trim(input('post.status',0));
			$data['add_time'] = time();
			$res = Db::table('oyk_school_class')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$school = Db::table('oyk_school')->where('status',1)->select();
			View::assign([
				'school' => $school
			]);
			return View::fetch();
		}
	}
	public function class_edit(){
		$req = request();
		if($req->isPost()){
			$id = trim(input('post.id'));
			$data['title'] = trim(input('post.title'));
			if(empty($data['title'])){
				$this->returnCode(1,'标题不能为空');
			}
			$data['school_id'] = (int)trim(input('post.school_id',0));
			if(empty($data['school_id'])){
				$this->returnCode(1,'请选择学校');
			}
			$data['address'] = trim(input('post.address'));
			if(empty($data['address'])){
				$this->returnCode(1,'请输入上课地址');
			}
			$data['tuition'] = (int)trim(input('post.tuition',0));
			$data['people_num'] = (int)trim(input('post.people_num',0));
			$data['teacher_name'] = trim(input('post.teacher_name'));
			if(empty($data['teacher_name'])){
				$this->returnCode(1,'老师姓名不能为空');
			}
			$data['teacher_phone'] = trim(input('post.teacher_phone'));
			if(empty($data['teacher_phone'])){
				$this->returnCode(1,'老师电话不能为空');
			}
			$data['class_hour'] = (int)trim(input('post.class_hour',0));
			$data['class_start_date'] = trim(input('post.class_start_date'));
			if(empty($data['class_start_date'])){
				$this->returnCode(1,'请选择开始上课日期');
			}
			$data['class_time'] = trim(input('post.class_time'));
			if(empty($data['class_time'])){
				$this->returnCode(1,'请输入上课周期');
			}
			$data['class_end_date'] = trim(input('post.class_end_date'));
			if(empty($data['class_end_date'])){
				$this->returnCode(1,'请选择报名截止日期');
			}
			$pic = input('post.pic');
			if(!empty($pic)){
				$data['intr'] = implode(';',$pic);
			}
			$data['status'] = (int)trim(input('post.status',0));
			$res = Db::table('oyk_school_class')->where('id',$id)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$id = input('get.id');
			$find = Db::table('oyk_school_class')->where('id',$id)->find();
			if(!empty($find['intr'])){
				$find['intr_s'] = explode(';',$find['intr']);
			}
			$school = Db::table('oyk_school')->where('status',1)->select();
			View::assign([
				'find' => $find,
				'school' => $school
			]);
			return View::fetch();
		}
	}
	public function enroll_list(){
		$id = input('get.id');
		$lists = Db::table('oyk_user_enroll')->where('class_id',$id)->order('add_time DESC,enroll_id DESC')->select();
		View::assign([
			'id' => $id,
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function enroll_add(){
		$req = request();
		if($req->isPost()){
			$data['class_id'] = trim(input('post.id'));
			if(empty($data['class_id'])){
				$this->returnCode(1,'班级ID不能为空');
			}
			$data['uid'] = trim(input('post.uid'));
			if(empty($data['uid'])){
				$this->returnCode(1,'用户ID不能为空');
			}
			$data['baby_name'] = trim(input('post.baby_name'));
			if(empty($data['baby_name'])){
				$this->returnCode(1,'孩子姓名不能为空');
			}
			$data['baby_school'] = trim(input('post.baby_school'));
			if(empty($data['baby_school'])){
				$this->returnCode(1,'孩子学校不能为空');
			}
			$data['baby_class'] = trim(input('post.baby_class'));
			if(empty($data['baby_class'])){
				$this->returnCode(1,'孩子班级不能为空');
			}
			$data['parent_name'] = trim(input('post.parent_name'));
			if(empty($data['parent_name'])){
				$this->returnCode(1,'家长姓名不能为空');
			}
			$data['parent_phone'] = trim(input('post.parent_phone'));
			if(empty($data['parent_phone'])){
				$this->returnCode(1,'家长电话不能为空');
			}
			$data['add_time'] = time();
			$res = Db::table('oyk_user_enroll')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			$id = input('get.id');
			View::assign([
				'id' => $id
			]);
			return View::fetch();
		}
	}
	public function enroll_edit(){
		$req = request();
		if($req->isPost()){
			$enroll_id = trim(input('post.enroll_id'));
			$data['baby_name'] = trim(input('post.baby_name'));
			if(empty($data['baby_name'])){
				$this->returnCode(1,'孩子姓名不能为空');
			}
			$data['baby_school'] = trim(input('post.baby_school'));
			if(empty($data['baby_school'])){
				$this->returnCode(1,'孩子学校不能为空');
			}
			$data['baby_class'] = trim(input('post.baby_class'));
			if(empty($data['baby_class'])){
				$this->returnCode(1,'孩子班级不能为空');
			}
			$data['parent_name'] = trim(input('post.parent_name'));
			if(empty($data['parent_name'])){
				$this->returnCode(1,'家长姓名不能为空');
			}
			$data['parent_phone'] = trim(input('post.parent_phone'));
			if(empty($data['parent_phone'])){
				$this->returnCode(1,'家长电话不能为空');
			}
			$data['status'] = (int)trim(input('post.status'));
			$res = Db::table('oyk_user_enroll')->where('enroll_id',$enroll_id)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$enroll_id = input('get.enroll_id');
			$find = Db::table('oyk_user_enroll')->where('enroll_id',$enroll_id)->find();
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	public function ad_list(){
		$lists = Db::table('oyk_ad')->order('sort DESC,id DESC')->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function ad_add(){
		$req = request();
		if($req->isPost()){
			$data['title'] = trim(input('post.title'));
			$data['img'] = trim(input('post.pic'));
			$data['url'] = trim(input('post.url'));
			$data['status'] = trim(input('post.status',1));
			$data['sort'] = trim(input('post.sort',0));
			$data['position'] = trim(input('post.position',1));
			$res = Db::table('oyk_ad')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	public function ad_edit(){
		$req = request();
		if($req->isPost()){
			$id = (int)input('post.id');
			$data['title'] = trim(input('post.title'));
			$data['img'] = trim(input('post.pic'));
			$data['url'] = trim(input('post.url'));
			$data['status'] = trim(input('post.status',1));
			$data['sort'] = trim(input('post.sort',0));
			$data['position'] = trim(input('post.position',1));
			$res = Db::table('oyk_ad')->where('id',$id)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$id = (int)input('get.id');
			$find = Db::table('oyk_ad')->where('id',$id)->find();
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	public function school_list(){
		$lists = Db::table('oyk_school')->order('add_time DESC,school_id DESC')->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function school_add(){
		$req = request();
		if($req->isPost()){
			$data['school_name'] = trim(input('post.school_name'));
			$data['name'] = trim(input('post.name'));
			$data['phone'] = trim(input('post.phone'));
			$data['license'] = trim(input('post.pic'));
			$data['status'] = (int)trim(input('post.status',2));
			$data['uid'] = (int)trim(input('post.uid',0));
			$data['add_time'] = time();
			$res = Db::table('oyk_school')->insert($data);
			if(!$res){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	public function school_edit(){
		$req = request();
		if($req->isPost()){
			$school_id = (int)input('post.school_id');
			$data['school_name'] = trim(input('post.school_name'));
			if(empty($data['school_name'])){
				$this->returnCode(1,'企业(学校)名不能为空');
			}
			$data['name'] = trim(input('post.name'));
			if(empty($data['name'])){
				$this->returnCode(1,'联系人不能为空');
			}
			$data['phone'] = trim(input('post.phone'));
			$data['license'] = trim(input('post.pic'));
			$data['status'] = (int)trim(input('post.status',2));

			$res = Db::table('oyk_school')->where('school_id',$school_id)->update($data);
			if(!$res){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$school_id = (int)input('get.school_id');
			$find = Db::table('oyk_school')->where('school_id',$school_id)->find();
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	public function school_del(){
		$school_id = (int)input('post.school_id');
		$res = Db::table('oyk_school')->where('school_id',$school_id)->update(['status'=>9]);
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
}